package com.su.gulimall.search.thread;

import java.util.concurrent.*;

public class CompletableFutureTest {
  public static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            5,200,10,
            TimeUnit.SECONDS,new LinkedBlockingDeque<>(100000),
            Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main线程开始-------");
     /*   //runnable,executor
        System.out.println("main线程开始-------");
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果为：" + i);
        }, executor);*/

      //有返回值，executor
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果为：" + i);
            return i;
        }, executor).whenComplete((res,exception)->{
            //虽然能得到异常信息，没法修改返回数据
            System.out.println("异步任务完成了---结果是："+res+";异常是："+exception);
        }).exceptionally(throwable -> {
            //可以感知异常，同时返回默认值
            return 10;
        });
        Integer integer = future.get();
        //异步编排功能
        System.out.println(integer);
        System.out.println("main线程结束-------");
    }
    //方式一：继承Thread
    public static class Thread01 extends Thread{
        @Override
        public void run() {
            System.out.println("当前线程:"+Thread.currentThread().getId());
            int i=10/2;
            System.out.println("运行结果为："+i);
        }
    }
    //方式二:实现Runnable
    public static class  Thread02 implements Runnable{

        @Override
        public void run() {

            System.out.println("当前线程:"+Thread.currentThread().getId());
            int i=10/2;
            System.out.println("运行结果为："+i);

        }
    }
    //方式三:实现Callable
    public  static  class  Thread03 implements Callable {

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程:"+Thread.currentThread().getId());
            int i=10/2;
            return i;
        }
    }
}